package com.huynv.thetopnews;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.InetAddress;
public class NetworkUtil {
     
    public static boolean getConnectivityStatusString() {
        //int conn = getConnectivityStatus(context);
        //String status = null;
        if (getUrl()) {
            return true;//status = "Wifi enabled";
        } else {
        	return false;//status = "Not connected to Internet";
        }
    }
    public static boolean getUrl(){	
    	try {
            URL url = new URL("http://thetopnews.net/Home/IsOnline");
            Log.e("fetchJSON", "ok0");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(10000);
            conn.setConnectTimeout(15000);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            //conn.setRequestProperty(field, newValue)
            // Starts the query
            Log.e("fetchJSON", "ok1");
            conn.connect();
            Log.e("fetchJSON", "ok2");
	         //InputStream stream = conn.getInputStream();
            InputStreamReader reader = null;
	         StringBuilder builder = new StringBuilder();

	         try {
	             // ...
	             reader = new InputStreamReader(conn.getInputStream(), "UTF-8");
	             char[] buffer = new char[8192];

	             for (int length = 0; (length = reader.read(buffer)) > 0;) {
	                 builder.append(buffer, 0, length);
	                 
	             }
	         } finally {
	             if (reader != null) try { reader.close(); } catch (IOException logOrIgnore) {}
	         }
	         Log.e("fetchJSON", "ok3");
	         String html = builder.toString();	 
	         Log.e("fetchJSON", "ok4");
	         String data = html;//convertStreamToString(stream);
      		 return true;     		 
      		 //stream.close();
         } catch (Exception e) {
        	 return false;
            //e.printStackTrace();
         }
        //return false;
	}
}
